package com.beordie.service;

import com.beordie.dao.BillMapper;
import com.beordie.model.Bill;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description 账单的服务层
 * @Date 2021/8/3 9:51
 * @Created 30500
 */
@Service
public class BillService {
    @Autowired
    private BillMapper billMapper;

    /**
     * @description 获取全部信息
     * @author 30500
     * @date 2021/8/3 12:23
     * @type [com.beordie.model.Bill]
     * @return java.util.List<com.beordie.model.Bill>
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Bill> getAll(Bill bill) {
        List<Bill> billList = billMapper.getAll(bill);
        return billList;
    }

    /**
     * @description 添加一条记录
     * @author 30500
     * @date 2021/8/3 12:23
     * @type [com.beordie.model.Bill]
     * @return int
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public int add(Bill bill) {
        int insert = billMapper.insert(bill);
        return insert;
    }

    /**
     * @description 根据主键进行查询
     * @author 30500
     * @date 2021/8/3 12:23
     * @type [int]
     * @return com.beordie.model.Bill
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Bill selectById(int id) {
        Bill bill = billMapper.selectByPrimaryKey(id);
        return bill;
    }

    /**
     * @description 根据主键删除一条数据
     * @author 30500
     * @date 2021/8/3 20:06
     * @type [int]
     * @return int
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public int delete(int id) {
        int delete = billMapper.deleteByPrimaryKey(id);
        return delete;
    }

    /**
     * @description 更新数据
     * @author 30500
     * @date 2021/8/3 20:23
     * @type [com.beordie.model.Bill]
     * @return int
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public int update(Bill bill) {
        int update = billMapper.updateByPrimaryKeySelective(bill);
        return update;
    }

    /**
     * @description 多条件分页查询
     * @author 30500
     * @date 2021/8/3 20:39
     * @type [int, int, com.beordie.model.Bill]
     * @return com.github.pagehelper.PageInfo<com.beordie.model.Bill>
     */
    public PageInfo<Bill> billPageInfo(int pageNum, int pageSize, Bill bill) {
        PageHelper.startPage(pageNum, pageSize);
        List<Bill> billList = billMapper.getAll(bill);
        return new PageInfo<>(billList);
    }
}
